package com.midcent.networklib.interfac;

import android.content.Context;
import com.android.volley.AuthFailureError;
import com.android.volley.ext.HttpCallback;
import com.android.volley.ext.RequestInfo;
import com.android.volley.ext.tools.HttpTools;
import com.midcent.networklib.manager.HttpRequestNetManager;
import com.midcent.networklib.utils.network.NetUtils;
import org.json.JSONObject;
import java.io.File;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by xiangke on 2016/1/15.
 */
public class HttpUploadRequestTools {

    protected Context mContext;
    protected String baseurl = HttpRequestNetManager.getServerHost();
    protected JSONObject postpara = new JSONObject();
    protected String url = null;
    protected String TAG = this.getClass().getName();
    protected RequestListener requestlistener;

    public HttpUploadRequestTools(Context context) {
        this.mContext = context;
    }


    public Map<String, String> getHeaders() throws AuthFailureError {
        return HttpRequestNetManager.getInstance().getHeaders(Long.toString(System.currentTimeMillis()));
    }



    public void addCommentRequst(String url, String userid, String orderdetailid, String firstcomment, String score, ArrayList<String> mPhotoList, HttpCallback callback) {
        if (!NetUtils.isConnected(mContext)) {
            return;
        }
        HttpTools httptool = new HttpTools(mContext);

        RequestInfo requestinfo = new RequestInfo();
        requestinfo.url = url;
        requestinfo.put("userid", userid);
        requestinfo.put("orderdetailid", orderdetailid);
        requestinfo.put("firstcomment", firstcomment);
        requestinfo.put("score", score);
        if (mPhotoList != null) {
            for (int i = 0; i < mPhotoList.size(); i++) {
                String path = mPhotoList.get(i);
                File temp = new File(path);
                requestinfo.put("file", temp);
            }
        }
        try {
            requestinfo.headers = getHeaders();
        } catch (AuthFailureError authFailureError) {
            authFailureError.printStackTrace();
        }
        httptool.upload(requestinfo, callback);

    }


    public void adduserHeaderRequst(String url, String userid, String mPhotoList, HttpCallback callback) {
        if (!NetUtils.isConnected(mContext)) {
            return;
        }
        HttpTools httptool = new HttpTools(mContext);

        RequestInfo requestinfo = new RequestInfo();
        requestinfo.url = url;
        requestinfo.put("userid", userid);
        if (mPhotoList != null) {
            String path = mPhotoList;
            File temp = new File(path);
            requestinfo.put("file", temp);
        }
        try {
            requestinfo.headers = getHeaders();
        } catch (AuthFailureError authFailureError) {
            authFailureError.printStackTrace();
        }
        httptool.upload(requestinfo, callback);
    }


    public void addAfterOrder(String url, String userid, int orderid, int goodsid, String reason, double price, int type, int usertype,
                              int receive, String description, ArrayList<String> mPhotoList, String goodsnumber, HttpCallback callback) {
        if (!NetUtils.isConnected(mContext)) {
            return;
        }
        HttpTools httptool = new HttpTools(mContext);
        RequestInfo requestinfo = new RequestInfo();
        requestinfo.url = url;
        requestinfo.put("userid", userid);
        requestinfo.put("orderid", orderid + "");
        requestinfo.put("goodsid", goodsid + "");
        requestinfo.put("reason", reason + "");
        requestinfo.put("type", type + "");
        requestinfo.put("usertype", usertype + "");
        requestinfo.put("receive", receive + "");
        requestinfo.put("price", price + "");
        requestinfo.put("description", description);
        if (goodsnumber != null) {
            requestinfo.put("goodsnumber", goodsnumber);
        }

        if (mPhotoList != null) {
            for (int i = 0; i < mPhotoList.size(); i++) {
                String path = mPhotoList.get(i);
                File temp = new File(path);
                requestinfo.put("file", temp);
            }
        }
        try {
            requestinfo.headers = getHeaders();
        } catch (AuthFailureError authFailureError) {
            authFailureError.printStackTrace();
        }
        httptool.upload(requestinfo, callback);
    }

    public void updateAfterOrder(String url, String userid, int orderid, int goodsid, String reason, double price, int applicationid,
                                 int receive, String description, ArrayList<String> mPhotoList, String goodsnumber, HttpCallback callback) {
        if (!NetUtils.isConnected(mContext)) {
            return;
        }
        HttpTools httptool = new HttpTools(mContext);
        RequestInfo requestinfo = new RequestInfo();
        requestinfo.url = url;
        requestinfo.put("applicationid", applicationid + "");
        requestinfo.put("userid", userid);
        requestinfo.put("orderid", orderid + "");
        requestinfo.put("goodsid", goodsid + "");
        requestinfo.put("reason", reason + "");
        requestinfo.put("receive", receive + "");
        requestinfo.put("price", price + "");
        requestinfo.put("description", description);
        if (goodsnumber != null) {
            requestinfo.put("goodsnumber", goodsnumber);
        }

        if (mPhotoList != null) {
            for (int i = 0; i < mPhotoList.size(); i++) {
                String path = mPhotoList.get(i);
                File temp = new File(path);
                requestinfo.put("file", temp);
            }
        }
        try {
            requestinfo.headers = getHeaders();
        } catch (AuthFailureError authFailureError) {
            authFailureError.printStackTrace();
        }
        httptool.upload(requestinfo, callback);
    }


    public void uploadMucPhotos(ArrayList<String> photoInfos, String userid, String url, HttpCallback callback) {
        if (!NetUtils.isConnected(mContext)) {
            return;
        }
        HttpTools httptool = new HttpTools(mContext);
        RequestInfo requestinfo = new RequestInfo();
        requestinfo.url = url;
        requestinfo.put("userid", userid);
        if (photoInfos != null) {
            for (int i = 0; i < photoInfos.size(); i++) {
                String path = photoInfos.get(i);
                File temp = new File(path);
                requestinfo.put("pic_file", temp);
            }
        }
        try {
            requestinfo.headers = getHeaders();
        } catch (AuthFailureError authFailureError) {
            authFailureError.printStackTrace();
        }
        httptool.upload(requestinfo, callback);
    }

}
